package com.example.demo.api;

import com.example.demo.model.Response;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import com.example.demo.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ApiController {

    @Autowired
    private UserService userService;

    /**
     * 注册用户
     * @param phone
     * @param password
     * @return
     */
    @RequestMapping(value = "/registorUser", method = RequestMethod.POST)
    public ResponseEntity<Object> registorUserAction(@RequestParam String userName, @RequestParam String password) {
        boolean success = userService.registorUserAction(userName, password);
        Response response = Util.initSuccessResponse(success);
        if (!success) {
            response = Util.initfailResponse("1000", "帐号已注册");
        }
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     * 帐号登录
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<Object> loginByUserName(@RequestParam String userName, @RequestParam String password) {
        Response response = userService.loginByUserNameAction(userName, password);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     * 更新用户信息
     * @param userId
     * @param phone
     * @param password
     * @return
     */
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public ResponseEntity<Object> updateUserInfoAction(String userId, String userName, String password) {
        boolean success = userService.updateUserInfoAction(userId, userName, password);
        Response response = Util.initSuccessResponse(success);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     * 查询用户列表
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    public ResponseEntity<Object> getInfoAction() {
        List<User> list = userService.getUserList();
        Response response = Util.initSuccessResponse(list);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}
